package com.example.xiaoxueqixiangmu.jiemian;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.xiaoxueqixiangmu.R;
import com.example.xiaoxueqixiangmu.utils.Constant;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MyAccount extends AppCompatActivity {
    private static final String TAG="MyAccount";
    private OkHttpClient client=new OkHttpClient();

    private Button btn_account_save;
    private TextView post_txt,tel_txt,name_txt,ac_username,ZJ_txt,ZJid_txt;
    private int id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_account);

        ActionBar actionBar=getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        btn_account_save=(Button)findViewById(R.id.btn_account_save);
        post_txt=(TextView)findViewById(R.id.post_txt);
        tel_txt=(TextView)findViewById(R.id.tel_txt);
        name_txt=(TextView)findViewById(R.id.name_txt);
        ac_username=(TextView)findViewById(R.id.ac_username);
        ZJ_txt=(TextView)findViewById(R.id.ZJ_txt);
        ZJid_txt=(TextView)findViewById(R.id.ZJid_txt);

        post_txt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder1=new AlertDialog.Builder(MyAccount.this);
                builder1.setTitle("请选择乘客类型");
                final String[] items=new String[]{"成人","儿童","学生","其他"};
                builder1.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        id=which;
                    }
                });
                builder1.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        post_txt.setText(items[id]);
                    }
                });
                builder1.setNegativeButton("取消",null);
                builder1.create().show();
            }
        });
        tel_txt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final TextDialog textDialog=new TextDialog(MyAccount.this);
                textDialog.setIcon(R.mipmap.ic_launcher);
                textDialog.setTitle("请输入证件号码");
                textDialog.show();

                View textEntryView = textDialog.getNameDialog();
                Button name_ok=(Button)textEntryView.findViewById(R.id.name_ok);
                Button name_cancel=(Button)textEntryView.findViewById(R.id.name_cancel);
                final EditText edit_name=(EditText) textEntryView.findViewById(R.id.edit_name);
                name_ok.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        tel_txt.setText(edit_name.getText());
                        textDialog.dismiss();
                    }
                });
                name_cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        textDialog.dismiss();
                    }
                });
            }
        });

        btn_account_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(){
                    @Override
                    public void run() {
                        super.run();
                        SharedPreferences preferences=MyAccount.this.getSharedPreferences("userinfo", Context.MODE_PRIVATE);
                        String value=preferences.getString("cookie","");
                        final String type= post_txt.getText().toString().trim();
                        final String tel=tel_txt.getText().toString().trim();
                        try {
                            RequestBody requestBody=new FormBody.Builder()
                                    .add("乘客类型",type)
                                    .add("电话",tel)
                                    .add("action","update")
                                    .build();
                            Request request=new Request.Builder()
                                    .addHeader("Cookie",value)
                                    .url(Constant.Host+"/otn/Account")
                                    .post(requestBody)
                                    .build();

                            Response response=client.newCall(request).execute();
                            String result=response.body().string();
                        }catch (IOException e){
                            e.printStackTrace();
                        }
                    }
                }.start();
                Toast.makeText(MyAccount.this,"保存成功！",Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        new Thread(){
            @Override
            public void run() {
                super.run();
                SharedPreferences preferences=MyAccount.this.getSharedPreferences("userinfo", Context.MODE_PRIVATE);
                String value=preferences.getString("cookie","");
                try {
                    Request request=new Request.Builder()
                            .addHeader("Cookie",value)
                            .url(Constant.Host+"/otn/Account")
                            .build();
                    Response response=client.newCall(request).execute();
                    String result=response.body().string();
                    //解析数据
                    JSONObject jsonObject=new JSONObject(result);

                    ac_username.setText(jsonObject.getString("username"));
                    name_txt.setText(jsonObject.getString("name"));
                    ZJ_txt.setText(jsonObject.getString("idType"));
                    ZJid_txt.setText(jsonObject.getString("id"));
                    post_txt.setText(jsonObject.getString("type"));
                    tel_txt.setText(jsonObject.getString("tel"));
                }catch (IOException e){
                    e.printStackTrace();
                }catch (JSONException e){
                    e.printStackTrace();
                }
            }
        }.start();
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                break;
            default:
        }
        return true;
    }
}